package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user/addressBook")
public class AddressBookController {

    private final AddressBookService addressBookService;

    //查询当前登录用户的所有地址信息
    @GetMapping("/list")
    public Result<List<AddressBook>> getAll() {
        log.info("查询当前登录用户的所有地址信息:{}", BaseContext.getCurrentId());
        return Result.success(addressBookService.getAll());
    }

    //新增地址
    @PostMapping()
    public Result add(@RequestBody AddressBook addressBook) {
        log.info("新增地址 用户:{},地址:{}",BaseContext.getCurrentId(), addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }

    //设置默认地址
    @PutMapping("/default")
    public Result setDefaltAddress(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址:{}",addressBook.getId());
        addressBookService.setDefaltAddress(addressBook.getId());
        return Result.success();
    }

    //根据id查询地址
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id) {
        log.info("根据id查询地址:{}",id);
        return Result.success(addressBookService.getById(id));
    }

    //根据id删除地址
    @DeleteMapping()
    public Result deleteById(Long id) {
        log.info("根据id删除地址:{}",id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    //根据id修改地址
    @PutMapping()
    public Result update(@RequestBody AddressBook addressBook) {
        log.info("根据id修改地址:{}",addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }

    //查询默认地址
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress() {
        log.info("查询默认地址");
        return Result.success(addressBookService.getDefaultAddress(BaseContext.getCurrentId()));
    }
}
